const db = uniCloud.database();
const { config } = require('../../config/index.js');
module.exports = async function() {
	const token = this.getUniIdToken();
	if (!token) {
		throw new Error('非法访问');
	}
	const [userRes, configRes] = await Promise.all([
		db.collection('uni-id-users').where({ token }).field({ setting: true }).get(),
		config()
	])
	const { setting } = configRes;
	const userSettingData = userRes.data.length ? userRes.data[0] : {};
	if (!userSettingData.setting || JSON.stringify(userSettingData.setting) == '{}') {
		return {
			code: 0,
			data: setting,
			msg: '获取成功'
		}
	} else {
		const userSettingArr = Object.keys(userSettingData.setting).map(key => ({
			key: key,
			value: userSettingData.setting[key]
		}));
		setting.forEach(item => {
			if (item.type != 'SHORTCUT_KEY') {
				item.list.forEach(item2 => {
					const find = userSettingArr.find(item3 => item3.key == item2.key);
					if (find) {
						item2.value = find.value;
					}
				})
			}
		})
		return {
			code: 0,
			data: setting,
			msg: '获取成功'
		}
	}
}